package com.elecbook.content.service;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elecbook.base.dto.PageDTO;
import com.elecbook.base.exception.BaseInfoException;
import com.elecbook.content.mapper.ContributeMapper;
import com.elecbook.content.mapper.ContributejudgeMapper;
import com.elecbook.content.model.dto.ContributeJudgePageDTO;
import com.elecbook.content.model.dto.ContributeJudgeSubmitDTO;
import com.elecbook.content.model.dto.ContributejudgePQuery;
import com.elecbook.content.model.po.Contribute;
import com.elecbook.content.model.po.Contributejudge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Z-ken
 * @since 2024/11/05 15:50:50
 */
@Service
public class ContributejudgeServiceImpl extends ServiceImpl<ContributejudgeMapper, Contributejudge> implements ContributejudgeService {

    @Autowired
    ContributejudgeMapper contributejudgeMapper;
    @Autowired
    ContributeMapper contributeMapper;

    @Override
    @Transactional
    public Contributejudge submitJudge(ContributeJudgeSubmitDTO dto) {
        Contributejudge contributejudge = contributejudgeMapper.selectById(dto.getContributeJudgeId());
        if (contributejudge == null || dto.getJudgeResult() == null) {
            throw new BaseInfoException("你提交的信息有误，尝试刷新或联系后台");
        }
        Contribute contribute = contributeMapper.selectById(contributejudge.getContributeId());
        contribute.setJudgeStatus(dto.getJudgeResult());
        contributejudge.setJudgeStatus(dto.getJudgeResult());
        contributejudgeMapper.updateById(contributejudge);
        contributeMapper.updateById(contribute);
        return contributejudge;
    }

    @Override
    public PageDTO<Contributejudge> queryContributeJudgePage(ContributeJudgePageDTO pageDTO) {
        // 额外自定义逻辑

        // 1.添加排序规则
        // 使用内置的函数转换并且 设置规则
        List<OrderItem> orderItems = pageDTO.toMpOrderItems();

        // 获取分页对象和排序情况
        Page<Contributejudge> page = pageDTO.toMpPageDefaultSortByIdAsc(orderItems.toArray(new OrderItem[0]));
        /*
        查询, 自定义的分页查询
         */
        Page<Contributejudge> p = baseMapper.selectPageVO(page, pageDTO);

        // 获取数据信息
        return PageDTO.of(p, Contributejudge.class);
    }

    @Override
    public Contributejudge getContributeJudgeById(Integer contributeJudgeId) {
        return baseMapper.selectById(contributeJudgeId);
    }


    /**
     * 基础分页请求--管理员
     */
    @Override
    public PageDTO<Contributejudge> pageQuery(ContributejudgePQuery query) {
        // 1.添加排序规则
        // 使用内置的函数转换并且 设置规则-这里为了方便扩展和使用先进行创建
        List<OrderItem> orderItems = query.toMpOrderItems();

        // 获取分页对象和排序情况
        Page<Contributejudge> page = query.toMpPageDefaultSortByIdAsc(orderItems.toArray(new OrderItem[0]));
    /*
    查询
    */
        Page<Contributejudge> p = baseMapper.selectPageEntity(page, query);

        // 获取数据信息
        return PageDTO.of(p, Contributejudge.class);
    }


    @Override
    public Contributejudge saveContributejudge(Contributejudge contributejudge) {
        contributejudge.setId(null);
        baseMapper.insert(contributejudge);
        return contributejudge;
    }

    @Override
    public Contributejudge updateContributejudgeById(Contributejudge contributejudge) {
        baseMapper.updateById(contributejudge);
        return contributejudge;
    }

}
